﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OLMS.DAL;
using OLMS.BLL;

namespace OLMS
{
    public partial class Index : System.Web.UI.Page
    {
        //获取类型编号
        public int GenresID
        {
            set
            {
                ViewState["gid"] = value;
            }
            get
            {
                return Convert.ToInt32(ViewState["gid"]);
            }
        }

        public int PageNumber
        {
            set
            {
                ViewState["page"] = value;
            }
            get
            {
                return Convert.ToInt32(ViewState["page"]);              
            }
        }
        public int PageSize
        {
            get
            {
                return 10;
            }
        }
        public int PageCount
        {
            set
            {
                ViewState["count"] = value;
            }
            get
            {
                return Convert.ToInt32(ViewState["count"]);
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.GenresID = -1;
                lblGenre.Text = "全部";
                GenresBinder();
                PageNumber = 1;
                AlbumsBinder();
            }
        }
        //绑定类型
        private void GenresBinder()
        {
            List<Genres> list = GenreBLL.GetGenresList();
            list.Insert(0, new Genres() { GenreId = -1, Name = "全部", Description = "全部类型" });
            repGenres.DataSource = list;
            repGenres.DataBind();
        }

        private void AlbumsBinder()
        {
            //获取全部音乐信息
            List<Albums> albums = AlbumBLL.GetAlbumsList();
            //根据音乐名称关键字查询
            if (txtSearchKey.Text != string.Empty)
            {
                albums = albums.Where(a => a.Title.Contains(txtSearchKey.Text)).ToList();
            }
            //根据音乐类别编号查询
            if (GenresID != -1)
            {
                albums = albums.Where(a => a.GenreId == GenresID).ToList();
            }
            //对数据格式重整来符合显示需要
            var result = albums.Select(a => new
            {
                AlbumId = a.AlbumId,
                GenreId = a.GenreId,
                GenreName = GenreBLL.GetGenresById(a.GenreId).Name,
                ArtistId = a.ArtistId,
                ArtistName = ArtistBLL.GetArtistsById(a.ArtistId).Name,
                Price = a.Price,
                Title = a.Title,
                AlbumArtUrl = a.AlbumArtUrl
            }).ToList();
            //使用Skip()方法和Take()方法实现分页
            repAlbums.DataSource = result.Skip((PageNumber - 1) * PageSize).Take(PageSize).ToList();          
            repAlbums.DataBind();

            //计算总页数
            PageCount = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(result.Count) / 10));
            //设置分页信息
            lblPager.Text = string.Format("每页{0}条/共{1}条 第{2}页/共{3}页", PageSize, result.Count, PageNumber, PageCount);
            //设置按钮状态
            btnFirst.Enabled = !(PageNumber <= 1);
            btnPrev.Enabled = !(PageNumber <= 1);
            btnNext.Enabled = !(PageNumber >= PageCount);
            btnLast.Enabled = !(PageNumber >= PageCount);
        }

        protected void btnGenres_Command(object sender, CommandEventArgs e)
        {
            //选择类型
            this.GenresID = Convert.ToInt32(e.CommandArgument);
            if (this.GenresID == -1)
            {
                lblGenre.Text = "全部";
            }
            else
            {
                lblGenre.Text = GenreBLL.GetGenresById(this.GenresID).Name;
            }

            PageNumber = 1;
            AlbumsBinder();

        }
        protected void btnAddCart_Command(object sender, CommandEventArgs e)
        {
            int albumsId = Convert.ToInt32(e.CommandArgument);
            if (Session["user"] == null)
            {
                Response.Redirect("Login.aspx");
            }
            else
            {
                Users user = Session["user"] as Users;
                if (CartBLL.AddToCart(user.UserId, albumsId))
                {
                    Response.Redirect("Cart.aspx");
                }
                else
                {
                    Response.Write("<script>alert('购买音乐专辑失败');</script>");
                }
            }

        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            PageNumber = 1;
            AlbumsBinder();
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            PageNumber = 1;
            AlbumsBinder();
        }

        protected void btnPrev_Click(object sender, EventArgs e)
        {
            PageNumber--;
            AlbumsBinder();
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            PageNumber++;
            AlbumsBinder();
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            PageNumber = PageCount;
            AlbumsBinder();
        }

    }
}